package com.dts.evaluate.ui.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dts.evaluate.R;
import com.dts.evaluate.bean.MaterialDataBean;
import com.dts.evaluate.bean.ProjectDataBean;
import com.dts.evaluate.callback.BeanCallBack;
import com.dts.evaluate.common.Constants;
import com.dts.evaluate.controller.impl.DataImpl;
import com.dts.evaluate.controller.intf.DataInterface;
import com.dts.evaluate.ui.view.TableListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 办事指南-》应交材料
 * A simple {@link Fragment} subclass.
 * Use the {@link MaterialFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MaterialFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @Bind(R.id.material_listview)
    TableListView materialListview;
    @Bind(R.id.none_material_textview)
    TextView noneMaterialTextview;

    // TODO: Rename and change types of parameters
    private ProjectDataBean.ApproveProjectBean mParam1;
    private String mParam2;


    public MaterialFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MaterialFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MaterialFragment newInstance(ProjectDataBean.ApproveProjectBean param1, String param2) {
        MaterialFragment fragment = new MaterialFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = (ProjectDataBean.ApproveProjectBean) getArguments().getSerializable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_material, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        LayoutInflater inflater = LayoutInflater.from(getContext());

        //初始化应交材料listview
        View materialHeader = inflater.inflate(R.layout.material_header_layout, null);
        materialListview.addHeaderView(materialHeader);
        materialListview.setScales(new float[]{1f / 7, 4f / 7, 5f / 7, 6f / 7});
        final MaterialListAdapterr materialListAdapterr = new MaterialListAdapterr(getContext());
        materialListview.setAdapter(materialListAdapterr);

        if (mParam1 == null) {
            return;
        }
        String appProjectCodeId = mParam1.getAppprojectcodeid();
        //应交材料
        DataInterface dataInterface = new DataImpl();
        dataInterface.getKbsxData(Constants.KBSX_URL, Constants.TYPE_MATERIAL, appProjectCodeId, new BeanCallBack<MaterialDataBean>() {

            @Override
            public void onResponse(boolean isFromCache, MaterialDataBean materialDataBean, Request request, @Nullable Response response) {
                List<MaterialDataBean.MaterialBean> data = materialDataBean.getDatum();
                if (data != null) {
                    materialListAdapterr.setData(data);
                }
            }
        }, this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /**
     * 应交材料listview适配器
     */
    static class MaterialListAdapterr extends BaseAdapter {

        List<MaterialDataBean.MaterialBean> data = new ArrayList<>();
        private Context context;
        private LayoutInflater inflater;

        public MaterialListAdapterr(Context context) {
            this.context = context;
            inflater = LayoutInflater.from(context);
        }

        public void setData(List<MaterialDataBean.MaterialBean> data) {
            this.data.clear();
            this.data.addAll(data);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            MaterialDataBean.MaterialBean materialBean = data.get(position);
            MaterialViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.material_item_layout, null);
                viewHolder = new MaterialViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (MaterialViewHolder) convertView.getTag();
            }
            viewHolder.serialNumberTextview.setText(String.valueOf(position + 1));
            viewHolder.materialTextview.setText(Html.fromHtml(materialBean.getContentTitle()));
            viewHolder.electronicTextview.setText(String.valueOf(materialBean.getElectronNo()));
            viewHolder.paperTextview.setText(String.valueOf(materialBean.getPaperNo()));
            viewHolder.formTextview.setText(String.valueOf(materialBean.getSys_tb()));
            return convertView;
        }

        static class MaterialViewHolder {
            @Bind(R.id.serial_number_textview)
            TextView serialNumberTextview;
            @Bind(R.id.material_textview)
            TextView materialTextview;
            @Bind(R.id.electronic_textview)
            TextView electronicTextview;
            @Bind(R.id.paper_textview)
            TextView paperTextview;
            @Bind(R.id.form_textview)
            TextView formTextview;

            MaterialViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
